package com.xuanwei.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuanwei.core.annos.Positive;
import com.xuanwei.core.entity.bo.NameBo;
import com.xuanwei.core.entity.param.Coupon;
import com.xuanwei.core.entity.vo.CouponListVo;
import com.xuanwei.core.entity.vo.ProductSimpleVo;
import com.xuanwei.core.enums.CouponLimitType;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwCouponService;
import com.xuanwei.dao.entity.XwCoupon;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/7/17 14:03
 **/
@RestController
@RequestMapping("api/coupon")
@Validated
public class CouponController extends BasicController {

    private IXwCouponService couponService;

    public CouponController(IXwCouponService couponService) {
        this.couponService = couponService;
    }

    /**
     * 添加优惠券
     *
     * @return 响应
     */
    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    public ResultVO add(@Valid Coupon coupon) {
        if (coupon.getType() == 0) {
            if (coupon.getConditionAmount() == null || coupon.getConditionAmount().compareTo(coupon.getAmount()) < 0) {
                return ResultVO.failResult("门栏金额必须大于优惠券金额");
            }
        }

        if (coupon.getExpireType() == 0) {
            if (coupon.getExpireStartTime() == null || coupon.getExpireEndTime() == null) {
                return ResultVO.failResult("有效时间不能为空");
            }
        }
        if (coupon.getExpireType() == 1) {
            if (coupon.getExpireDays() == null || coupon.getExpireDays() < 1) {
                return ResultVO.failResult("领取天数格式错误");
            }
        }

        if (coupon.getLimitSendTotal() < coupon.getLimitPre()) {
            return ResultVO.failResult("发放量不能少于每人限制");
        }

        if (coupon.getLimitType().equals(CouponLimitType.LIMIT_ACTIVITY.getCode()) && coupon.getObjId() == null) {
            return ResultVO.failResult("请选择活动");
        }

        if (coupon.getLimitType().equals(CouponLimitType.LIMIT_PRODUCT.getCode()) && coupon.getObjId() == null) {
            return ResultVO.failResult("请选择商品");
        }
        couponService.add(coupon);

        return ResultVO.successResult();
    }

    @RequestMapping(value = "update.do", method = RequestMethod.GET)
    public ResultVO update(@Positive(message = "优惠券ID格式错误") Integer id,
                           @NotBlank(message = "优惠券说明错误") String explainText,
                           @Positive(message = "总量格式错误") Integer limitSendTotal,
                           @Positive(message = "每人限制格式错误") Integer limitPre) {
        couponService.update(id, explainText, limitSendTotal, limitPre);
        return ResultVO.successResult();
    }

    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    public ResultVO list(Integer type, Integer limitType, String title) {
        Page<XwCoupon> page = getPage();
        Page<CouponListVo> data = couponService.list(type, limitType, title, page);
        return ResultVO.successResult(data);
    }

    @RequestMapping(value = "updateStatus.do", method = RequestMethod.GET)
    public ResultVO updateStatus(@Min(value = 1, message = "id格式错误") Integer id) {

        couponService.updateStatus(id);
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    public ResultVO delete(@Min(value = 1, message = "id格式错误") Integer id) {

        couponService.delete(id);
        return ResultVO.successResult("删除成功");
    }

    @RequestMapping(value = "detail.do", method = RequestMethod.GET)
    public ResultVO detail(@Positive(message = "优惠券ID格式错误") Integer couponId) {
        XwCoupon byId = couponService.getById(couponId);
        return ResultVO.successResult(byId);
    }

    @RequestMapping(value = "productList.do", method = RequestMethod.GET)
    public ResultVO productList(Integer id, String name) {
        List<ProductSimpleVo> search = couponService.search(id, name);
        return ResultVO.successResult(search);
    }

    @RequestMapping(value = "activityList.do", method = RequestMethod.GET)
    public ResultVO activityList() {
        List<NameBo> search = couponService.findAllActivity();
        return ResultVO.successResult(search);
    }

}
